<template>
  <el-container class="main-container">
    <!-- 遮罩层 -->
    <div class="zhesss" ref="zhesss"></div>
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
        router
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img
              :src="userinfo.user_pic"
              class="avatar"
              v-if="userinfo.user_pic"
              alt=""
            />
            <img src="../../assets/logo.png" v-else alt="" class="avatar" />
            <span>个人中心</span>
          </template>
          <el-menu-item index="/staggerer/main/user-info"
            ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
          >
          <el-menu-item index="/staggerer/main/user-avatar"
            ><i class="el-icon-camera"></i>更换头像</el-menu-item
          >
          <el-menu-item index="/staggerer/main/user-pwd"
            ><i class="el-icon-key"></i>重置密码</el-menu-item
          >
        </el-submenu>
        <el-menu-item index="" @click="logout"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside width="200px"
        ><div class="user-box">
          <img
            :src="userinfo.user_pic"
            class="avatar"
            v-if="userinfo.user_pic"
            alt=""
          />
          <img src="../../assets/logo.png" v-else alt="" class="avatar" />
          <span>欢迎 {{ userinfo.nickname || userinfo.username }}</span>
        </div>
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="#23262E"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          router
        >
          <!-- 不包含子菜单的“一级菜单” -->
          <el-menu-item index="/staggerer/main/home"
            ><i class="el-icon-s-home"></i>首页</el-menu-item
          >
          <!-- 包含子菜单的“一级菜单” -->
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-s-order"></i>
              <span>文章管理</span>
            </template>
            <el-menu-item index="/staggerer/main/art-cate"
              ><i class="el-icon-s-data"></i>文章分类</el-menu-item
            >
            <el-menu-item index="/staggerer/main/art-list"
              ><i class="el-icon-document-copy"></i>文章列表</el-menu-item
            >
          </el-submenu>
          <!-- 包含子菜单的“一级菜单” -->
          <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-user-solid"></i>
              <span>个人中心</span>
            </template>
            <el-menu-item index="/staggerer/main/user-info"
              ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
            >
            <el-menu-item index="/staggerer/main/user-avatar"
              ><i class="el-icon-camera"></i>更换头像</el-menu-item
            >
            <el-menu-item index="/staggerer/main/user-pwd"
              ><i class="el-icon-key"></i>重置密码</el-menu-item
            >
          </el-submenu>
        </el-menu></el-aside
      >
      <el-container>
        <!-- 页面主体区域 -->
        <el-main class="el-container">
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer></el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import axios from '@/project/staggerer/Dsrc/main.js'
export default {
  name: 'Main',
  data() {
    return {
      menus: ''
    }
  },
  computed: {
    ...mapState('Ditem', ['userinfo', 'token'])
  },
  methods: {
    logout() {
      // 询问用户是否退出登录
      this.$confirm('您去人退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 执行退出登录操作
          this.$store.commit('Ditem/updatedToken', '')
          this.$router.push('/staggerer/logbox/login')
        })
        .catch(err => err)
    },
    // 初始化左侧产地的数据
    initMenus() {
      return axios
        .get('/my/menus', {
          headers: {
            Authorization: this.token
          }
        })
        .then(({ data: res }) => {
          if (res.code === 0) {
            this.menus = res.data
          }
        })
    }
  },
  async created() {
    await this.initMenus()
    this.$store.dispatch('Ditem/initUserInfo')
  }
}
</script>

<style lang="less" scoped>
.zhesss {
  position: fixed;
  z-index: 99999;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #000;
  opacity: 0.5;
  display: none;
}
// 全局重置 element-ui 组件库的基础样式
.el-input__inner,
.el-button,
.el-link,
.el-menu-item,
.el-submenu__title {
  font-size: 12px;
}
.el-menu.el-menu--horizontal {
  height: 60px;
  border-bottom: none;
}
// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
.main-container {
  min-height: 1335px;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
} // 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}
</style>
